package com.zbkj.crmeb.upload.service.impl;

import com.exception.CrmebException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.zbkj.crmeb.system.service.SystemAttachmentService;
import com.zbkj.crmeb.system.service.SystemConfigService;
import com.zbkj.crmeb.upload.service.QiNiuService;
import com.zbkj.crmeb.upload.vo.CloudVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * AsyncServiceImpl 同步到云服务
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class QiNiuServiceImpl implements QiNiuService {
    private static final Logger logger = LoggerFactory.getLogger(QiNiuServiceImpl.class);

    @Lazy
    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private SystemConfigService systemConfigService;

    private CloudVo cloudVo = new CloudVo();

    /**
     * 同步到七牛云
     *
     * @param cloudVo CloudVo
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @Async
    @Override
    public void upload(UploadManager uploadManager, CloudVo cloudVo, String upToken, String webPth, String localFile, Integer id) {
        try {
            logger.info("上传文件" + id + "开始：" + localFile);

            File file = new File(localFile);
            if (!file.exists()) {
                logger.info("上传文件" + id + localFile + "不存在：");
                return;
            }

            Response put = uploadManager.put(localFile, webPth, upToken);
            put.close();
            logger.info("上传文件" + id + " -- 结束：" + put.address);
            //更新数据库
            systemAttachmentService.updateCloudType(id, 2);
        } catch (QiniuException ex) {
            //TODO
            throw new CrmebException(ex.getMessage());
        }
    }

    @Override
    public String upload(MultipartFile file) {
        String result = "";
        // 生成上传凭证，然后准备上传
        setConf();
        Auth auth = Auth.create(cloudVo.getAccessKey(), cloudVo.getSecretKey());
        String token = auth.uploadToken(cloudVo.getBucketName());
        try {
// 文件名
            String fileName = file.getOriginalFilename();
            // 文件后缀
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            // 构造一个带指定Zone对象的配置类, 默认华东
            Configuration cfg = new Configuration(Region.huanan());
            String fileKey = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
            // 其他参数参考类注释
            UploadManager uploadManager = new UploadManager(cfg);
            uploadManager.put(file.getInputStream(), fileKey, token, null, null);
            result = cloudVo.getDomain() + "/" + fileKey;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private void setConf() {
        String pre = "qn";
        cloudVo.setDomain(systemConfigService.getValueByKeyException(pre + "UploadUrl"));
        cloudVo.setAccessKey(systemConfigService.getValueByKeyException(pre + "AccessKey"));
        cloudVo.setSecretKey(systemConfigService.getValueByKeyException(pre + "SecretKey"));
        cloudVo.setBucketName(systemConfigService.getValueByKeyException(pre + "StorageName"));
        cloudVo.setRegion(systemConfigService.getValueByKeyException(pre + "StorageRegion"));
    }
}

